package com.vitabuybuy.tv.ui.auth

import android.os.Bundle
import android.os.Handler
import androidx.lifecycle.Observer
import com.library.common.base.BaseActivity
import com.library.common.base.appContext
import com.library.common.ext.logE
import com.library.common.ext.mmkv
import com.library.common.toast.XToast
import com.library.common.util.JsonUtil
import com.library.common.util.MMKVContancts
import com.library.common.util.SdCardUtil
import com.vitabuybuy.tv.BuildConfig
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivitySplashBinding
import com.vitabuybuy.tv.ui.MainActivity
import com.vitabuybuy.tv.ui.tencent.liveroom.IMLVBLiveRoomListener
import com.vitabuybuy.tv.ui.tencent.liveroom.MLVBLiveRoom
import com.vitabuybuy.tv.ui.tencent.liveroom.roomutil.commondef.LoginInfo
import com.vitabuybuy.tv.ui.viewmodel.NoViewModel
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission

/**
 * 作者：SimGa
 * 日期：2019/5/8
 * 时间：09:48
 * 闪屏页
 */
class SplashActivity : BaseActivity<NoViewModel, ActivitySplashBinding>() {
    private var isFirsh = true
    override val layoutId: Int
        get() = R.layout.activity_splash
    override val isUseBaseTitleBar: Boolean
        get() = false

    override fun initView(savedInstanceState: Bundle?) {
        isFirsh = mmkv.decodeBool(MMKVContancts.IS_FIRSH_IN, true)
        AndPermission.with(this)
            .runtime()
            .permission(
                Permission.ACCESS_COARSE_LOCATION,
                Permission.ACCESS_FINE_LOCATION,
                Permission.READ_EXTERNAL_STORAGE,
                Permission.WRITE_EXTERNAL_STORAGE
            )
            .onDenied {
                if (AndPermission.hasAlwaysDeniedPermission(this, it)) {
                    // 这些权限被用户总是拒绝。
                    XToast.normal("无权限，请到权限中心打开相关权限")
                } else {
                    XToast.normal("无法获取权限，程序无法启动")
                }
                finish()
            }
            .onGranted {
                SdCardUtil.get(appContext).initFileDir()
                lunchBanner()
            }.start()
    }

    private fun lunchBanner() {
        val isLogin = mmkv.decodeBool(MMKVContancts.IS_LOGIN_IN, false)
        if (isLogin) {
            val loginInfo = LoginInfo().apply {
                sdkAppID = BuildConfig.LIVEAPPID.toLong()
                userID = mmkv.decodeInt(MMKVContancts.USER_ID, 0).toString()
                userSig = mmkv.decodeString(MMKVContancts.USER_SIGN, "")
                userName = mmkv.decodeString(MMKVContancts.USER_NICKNAME, "")
                userAvatar = mmkv.decodeString(MMKVContancts.USER_AVATAR, "")
            }
            MLVBLiveRoom.sharedInstance(appContext)
                .login(loginInfo, object : IMLVBLiveRoomListener.LoginCallback {
                    override fun onSuccess() {
                        jumpActivity()
                    }
                    override fun onError(errCode: Int, errInfo: String?) {
                        jumpActivity()
                        errInfo?.run {
                            "errCode ：$errCode ；errInfo：$this".logE()
                        }
                    }
                })
        } else {
            jumpActivity()
        }
    }

    private fun jumpActivity() {
        Handler().postDelayed({
            if (isFirsh) {
                GuideActivity.openActivity(mContext)
            } else {
                MainActivity.openActivity(mContext)
            }
            finish()
        }, 3000)
    }
}
